﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using Jungle.Code.Utils;
using Jungle.Code.Core;

using Jungle.Code.ProductManager;

namespace Jungle.Code.ProductManager.WebControls
{
    public class ProductsList: WebControl, IPostBackEventHandler
    {
        private int OnPage = 30;
        private int CurrPage = 1;

        //------------------------------------------------------------------------------------------------------
        public void RaisePostBackEvent(string eventArgument)
        {
            int pos = eventArgument.IndexOf(',');
            int l = eventArgument.Length;
            string s1 = eventArgument.Substring(0, pos);
            string s2 = eventArgument.Substring(pos + 1, (l - (pos + 1)));

            if (s1 == "chP") // перелистывание страницы
            {
                CurrPage = Convert.ToInt32(s2);
            }
            if (s1 == "delProduct") // удаление пользователя
            {
                Product p = new Product();
                p.ProductId = Convert.ToInt32(s2);
                p.Delete();
            }
            if (s1 == "hideProduct") 
            {
                Product p = new Product();
                p.ProductId = Convert.ToInt32(s2);
                p.Hide();
                
            }
            if (s1 == "unhideProduct")
            {
                Product p = new Product();
                p.ProductId = Convert.ToInt32(s2);
                p.UnHide();                
            }
            
        }
        //--------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            #region ProductsListTable
            string cmd = "";
            
            writer.AddAttribute("width", "100%");
            writer.AddAttribute("height", "100%");

            
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            writer.AddAttribute("bgcolor", "#CCCCCC");
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);            
            writer.AddAttribute("width", "30px");
            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("ID");
            writer.RenderEndTag();
            writer.RenderEndTag();            
            
            writer.AddAttribute("width", "150px");
            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("Каталожный номер");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("Название");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.AddAttribute("width", "100px");
            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("Цена");
            writer.RenderEndTag();
            writer.RenderEndTag();
            
            writer.AddAttribute("width", "150px");
            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("Дата добалвения");
            writer.RenderEndTag();
            writer.RenderEndTag();
            
            writer.AddAttribute("width", "100px");
            writer.AddAttribute("class", "products_table_td");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute("class", "products_table_title");
            writer.RenderBeginTag(HtmlTextWriterTag.Font);
            writer.Write("Действие");
            writer.RenderEndTag();
            writer.RenderEndTag();
            
            writer.RenderEndTag();

            //
            Product p = new Product();
            if (Context.Request["cmd"] != null)
            {
                cmd = Context.Request["cmd"];
                if(cmd == "view")
                    p.GetAllProducts();
                if(cmd == "viewHidden")
                    p.GetHiddenProducts();
            }
            
            bool bgChange = true;

            int count = 0;
            for (int i = (OnPage * CurrPage) - OnPage; i <= (OnPage * CurrPage) - 1; i++)
            {
                if (i >= p.ProductsList.Count) break;

                Product t = p.ProductsList[i];

                if (bgChange) { writer.AddAttribute("bgcolor", "#FFFFFF"); bgChange = false; } else { bgChange = true; }
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                writer.AddAttribute("align", "center");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_text");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                writer.Write(t.ProductId.ToString());
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute("align", "center");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_text");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                writer.Write(t.CatalogNumber.Trim());
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute("align", "center");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_text");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                writer.Write(t.Title.Trim());
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute("align", "center");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_text");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                writer.Write(t.Price.ToString());
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute("align", "center");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_text");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                writer.Write(t.AddDate.ToShortDateString());
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.AddAttribute("align", "center");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute("class", "products_table_text");
                writer.RenderBeginTag(HtmlTextWriterTag.Font);
                PrintBtnDel(writer, t.ProductId);
                PrintBtnEdit(writer, t.ProductId);
                if (cmd == "viewHidden") { PrintBtnHideUnhide(writer, t.ProductId, false); } else { PrintBtnHideUnhide(writer, t.ProductId, true); }
                writer.RenderEndTag();
                writer.RenderEndTag();

                writer.RenderEndTag();

                count++;
            }            
            //
            writer.RenderEndTag();

            if (p.ProductsList.Count % OnPage == 0)
                RenderPageLister(writer, p.ProductsList.Count / OnPage);
            else
                RenderPageLister(writer, (p.ProductsList.Count / OnPage) + 1);
            #endregion
            
            base.RenderContents(writer);
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnDel(HtmlTextWriter writer, int productId)
        {
            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;
            opt.Argument = "delProduct," + productId.ToString();

            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "if(confirm(\"Вы действительно хотите удалить?\"))" + cs.GetPostBackEventReference(opt));
            writer.AddAttribute("title", "Удалить");
            writer.AddAttribute("style", "cursor: pointer;");
            PrintImage(writer, "/Admin/img/btn_blue_delete.jpg");
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnEdit(HtmlTextWriter writer, int productId)
        {
            string linkStr = Context.Request.RawUrl;
            int t = linkStr.IndexOf("cmd");

            if (t != -1)
            {
                linkStr = linkStr.Substring(0, t+4);
            }

            writer.AddAttribute(HtmlTextWriterAttribute.Href, linkStr+ "edit&pId=" + productId.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.AddAttribute("title", "Редактировать");
            writer.AddAttribute("style", "cursor: pointer;");
            PrintImage(writer, "/Admin/img/btn_blue_edit.png");
            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnHideUnhide(HtmlTextWriter writer, int productId, bool flagHideUnhide)
        {
            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;
            
            if (flagHideUnhide)
            {
                opt.Argument = "hideProduct," + productId.ToString();
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "if(confirm(\"Вы действительно хотите сделать товар недоступным для посетителей?\"))" + cs.GetPostBackEventReference(opt));
                writer.AddAttribute("title", "Убрать из каталога");
                writer.AddAttribute("style", "cursor: pointer;");
                PrintImage(writer, "/Admin/img/btn_blue_hide.png");
            }
            else 
            {
                opt.Argument = "unhideProduct," + productId.ToString();
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "if(confirm(\"Вы действительно хотите сделать товар доступным для посетителей?\"))" + cs.GetPostBackEventReference(opt));
                writer.AddAttribute("title", "Вернуть в каталог");
                writer.AddAttribute("style", "cursor: pointer;");
                PrintImage(writer, "/Admin/img/btn_blue_unhide.jpg");            
            }
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintImage(HtmlTextWriter writer, string imgUrl)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + imgUrl);
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        private HtmlTextWriter RenderPageLister(HtmlTextWriter writer, int count)
        {
            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;

            writer.Write("<b>Страница: </b>");
            for (int i = 1; i <= count; i++)
            {
                if (i == CurrPage)
                    writer.Write("<b>" + i.ToString() + "</b>");
                else
                {
                    opt.Argument = "chP," + i.ToString();
                    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, cs.GetPostBackEventReference(opt));
                    writer.AddAttribute("style", "cursor: pointer;");
                    writer.RenderBeginTag(HtmlTextWriterTag.Span);
                    writer.Write(i.ToString());
                    writer.RenderEndTag();
                }
                if (i < count) writer.Write(", ");
            }
            return writer;
        }

    }   

}
